package com.starblink.android.guang.test

import android.graphics.Color
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.starblink.android.basic.adapter.BaseBindingVH
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.ext.inflater
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.android.basic.base.EmptyViewModel
import com.starblink.android.basic.base.activity.BaseTVMActivity
import com.starblink.android.guang.databinding.ActivityAdapterTestBinding
import com.starblink.android.guang.databinding.CellTestABinding
import com.starblink.android.guang.databinding.CellTestBBinding

class AdapterTestActivity : BaseTVMActivity<ActivityAdapterTestBinding, EmptyViewModel>() {
    override fun initViewBinding(): ActivityAdapterTestBinding {
        return ActivityAdapterTestBinding.inflate(layoutInflater)
    }


    private val list = arrayListOf<Any>().apply {
        for (i in 0..100) {
            when {
                i % 2 == 0 -> add(A("A$i"))
                else -> add(B("B$i"))
            }
        }
    }

    private val adapter: MutableAdapter by lazyOnNone {
        MutableAdapter(list).apply {
            addVhDelegate {
                AHolder(CellTestABinding.inflate(it.inflater, it, false))
            }
            addVhDelegate {
                BHolder(CellTestBBinding.inflate(it.inflater, it, false))
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewBinding.recycler.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        viewBinding.recycler.adapter = adapter
    }
}

private data class A(val text: String?)

private class AHolder(binding: CellTestABinding) :
    BaseBindingVH<A, CellTestABinding>(binding = binding) {
    override fun onBind(model: A, position: Int) {
        binding.text.text = model.text
        binding.text.setBackgroundColor(Color.BLUE)
    }

}

private data class B(val text: String?)

private class BHolder(binding: CellTestBBinding) :
    BaseBindingVH<B, CellTestBBinding>(binding) {
    override fun onBind(model: B, position: Int) {
        binding.text.text = model.text
        binding.text.setBackgroundColor(Color.DKGRAY)
    }

}